package com.spring.boot03.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.spring.boot03.bean.Emp;
import com.spring.boot03.bean.ResultCode;
import com.spring.boot03.bean.ResultTemplate;
import com.spring.boot03.service.EmpService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Resource
    private EmpService empService;

    @GetMapping("/find1")
    public ResultCode<List<Emp>> find1(String ename) {
        //条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        //开始设置条件(模糊查询)
        wrapper.like("ename", ename);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find2")
    public ResultCode<List<Emp>> find2(String job, Double sal) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(等于和大于)
        wrapper.eq("job", job);
        wrapper.gt("sal", sal);
        // wrapper.eq("job",job).gt("sal",sal);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find3")
    public ResultCode<List<Emp>> find3(Double sal1, Double sal2) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(between and)
        wrapper.between("sal", sal1, sal2);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find4")
    public ResultCode<List<Emp>> find4(Double sal, int mgr) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(小于和或者)
        wrapper.lt("sal", sal).or().eq("mgr", mgr);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find5")
    public ResultCode<List<Emp>> find5(Integer dept1, Integer dept2) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(在什么之中)
        wrapper.in("deptno", dept1, dept2);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find6")
    public ResultCode<List<Emp>> find6(String ename) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(在什么之中)
        wrapper.inSql("sal", "select sal from emp where ename=" + "'" + ename + "'");
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find7")
    public ResultCode<List<Emp>> find7(String sql) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(嵌套sql语句)
        wrapper.last(sql);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find8")
    public ResultCode<List<Emp>> find8(String name) {
        // 条件查询器
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        // 开始设置条件(字段名 )
        wrapper.select(name);
        List<Emp> list = empService.list(wrapper);
        return ResultTemplate.success(list);
    }

    @GetMapping("/find9")
    public ResultCode<List<Emp>> find9(@RequestParam(name = "page", defaultValue = "1") int page) {
        //分页查询，页码可指定
        PageHelper.startPage(page, 5);
        List<Emp> list = empService.list();
        return ResultTemplate.success(list);
    }

    @GetMapping("/find")
    public ResultCode<List<Emp>> find(Integer id) {
        return ResultTemplate.success(empService.find(id));
    }


}
